<template>
<master :title="title">
  <listpage ref="listpage"></listpage>
</master>
</template>
<script type="text/javascript">
(function (G) {
  var _this;
  G.vue({
  "usingComponents": {
    "master": "/components/Master/Master",
    "listpage": "/components/List/Page/Page"
  },
  "enablePullDownRefresh": true
},{
    data: {
      title: '',
      modelName: '',
      models: {},
      platform: G.platform
    },
    methods: {
      load: function (next) {
        this.selectComponent('#listpage').load(next);
      },
      refresh: function (next) {
        this.selectComponent('#listpage').refresh(next);
      },
      onPullDownRefresh: function (event) {
        this.refresh(function () {
          G.stopPullDownRefresh();
        })
      },
      onReachBottom: function (event) {
        this.load();
      },
      edit: function (event) {
        var modeleditquery = {
          values: {},
          refresh: this.refresh,
          models: this.getData('models'),
          url: '/api/model/' + this.getData('modelName')
        }
        var userNickName = G.Storage.get('nickNames').user || '用户';
        modeleditquery.models.name.rule = { NotNull: true,Name:true };
        if (event.type == 'edit') {
          modeleditquery.values = event.detail;
          modeleditquery.models.userName.type = '';
          modeleditquery.title = '修改' + userNickName + '信息';
          modeleditquery.meth = 'PUT';
        } else {
          modeleditquery.models.userName.type = 'input';
          modeleditquery.title = '新增' + userNickName;
          modeleditquery.meth = 'POST';
        }
        G.Storage.set('modeleditquery', modeleditquery);
        this.$go('/pages/model/edit/edit');
      }
    },
    mounted: function (query) {
      query = {
        modelName:'user',
        nickName:G.Storage.get('nickNames').user || '用户'
      }
      var title = query.nickName;
      var groupNickName = G.Storage.get('nickNames').group || '权限';
      var _this = this;
      G.ajax('/model/' + query.modelName, function (ret) {
        if (!ret.errorMsg) {
          _this.selectComponent('#listpage').init({
            getUrl: '/api/model/' + query.modelName + '?sql=userList_user',
            models: ret,
            searchKey: query.searchKey || 'name,unitId',
            edit: {
              status: '/api/model/' + query.modelName
            },
            actions: {
              add:{
                action:function(event){
                  _this.edit(event);
                },
                name:'新增'
              },
              edit:{
                action: function (event) {
                  _this.edit(event);
                },
                name: '修改'
              },
              editgroup:{
                className: 'danger',
                action: function(event){
                  G.$go('/pages/user/group/group?userId=' + event.detail.id + '&name=' + (event.detail.name || event.detail.nickName || event.detail.userName));
                },
                name: '分配' + groupNickName
              },
              resetpassword:{
                className: 'warning',
                action: function(event){
                  G.confirmx('确定要重置此' + query.nickName + '的密码?', function () {
                    G.ajax('/api/model/user', function (ret) {
                      if (!ret.errorMsg) G.alertx('重置密码成功!')
                    }, {
                        meth: 'PUT',
                        data: {
                          passWord: G.MD5('123456'),
                          id: event.detail.id
                        }
                      })
                  })
                },
                name: '重置密码'
              }
            }
          })
          _this.load();
          _this.setData({ models: ret, title: title, modelName: query.modelName });
        }
      }, {
          storageName: query.modelName + 'Model'
        })
    }
  });
})(Y)
</script>
<style scoped>

</style>